import com.yoke.util.FileUtil;
import com.yoke.util.StringUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * Created by jiangzeyin on 2017/6/27.
 */
public class WeiXin {
    //http://mp.weixin.qq.com/s/7J13gEuhulXQyNtKDqkrUA
    public static void main(String[] args) throws IOException {

        // CSSOMParser cssomParser = new CSSOMParser();
        //http://www.jbwljt.top/article/a335?hide_share_meta=1
        //http://mp.weixin.qq.com/s/7J13gEuhulXQyNtKDqkrUA
        Connection connection = Jsoup.connect("http://www.jbwljt.top/article/a335?hide_share_meta=1");
        Document document = connection.get();
        Elements scripts = document.getElementsByTag("script");
        scripts.remove();
        Elements style = document.getElementsByTag("style");
        style.forEach(element -> {
            String data = element.data();
            if (StringUtil.IsEmpty(data)) {
                element.remove();
                return;
            }
            //InputSource source = new InputSource();
            // source.setByteStream(new ByteArrayInputStream(data.getBytes()));
            //source.setEncoding("UTF-8");

            String[] css_s = StringUtil.StringToArray(data, "}");
            if (css_s == null || css_s.length <= 0) {
                element.remove();
                return;
            }
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder temp = new StringBuilder();
            boolean find_screen = false;
            for (String itemCss : css_s) {
                String queryCass = itemCss.substring(0, itemCss.indexOf("{"));
                String queryData = itemCss.substring(itemCss.indexOf("{"));
                System.out.println(itemCss);
                if (find_screen) {
                    if (StringUtil.IsEmpty(itemCss)) {
                        System.out.println("结束来");
                        find_screen = false;
                        System.out.println(temp);
                    }
                    temp.append(queryCass).append(queryData);
                    continue;
                } else {
                    // 响应跳过
                    if (queryCass.startsWith("@")) {
                        temp.append(queryCass).append(queryData);
                        find_screen = true;
                        continue;
                    }
                }

//                if () {
//                    stringBuilder.append(queryCass).append(queryData).append("}");
//                    continue;
//                }
                // 拆分多个样式
                String[] queryCass_s = StringUtil.StringToArray(queryCass, ",");
                if (queryCass_s == null || queryCass_s.length <= 0)
                    continue;
                if (queryCass_s.length == 1) {
                    // 判断空格
                    queryCass_s = StringUtil.StringToArray(queryCass, " ");
                    if (queryCass_s == null || queryCass_s.length <= 0)
                        continue;
                    if (queryCass_s.length == 1) {
                        queryCass_s = StringUtil.StringToArray(queryCass, ">");
                        if (queryCass_s == null || queryCass_s.length <= 0)
                            continue;
                        if (queryCass_s.length == 1) {
                            dod(document, queryCass_s[0], queryData, stringBuilder);
                        } else {
                            //分别判断
                            boolean flag = true;
                            for (String item : queryCass_s) {
                                Elements elements = document.select(item);
                                if (elements.isEmpty()) {
                                    flag = false;
                                    break;
                                }

                            }
                            if (flag) {
                                stringBuilder.append(queryCass).append(queryData).append("}");
                            }
                        }
                    } else {
                        //分别判断
                        boolean flag = true;
                        for (String item : queryCass_s) {
                            Elements elements = document.select(item);
                            if (elements.isEmpty()) {
                                flag = false;
                                // System.out.println("结束：" + item);
                                break;
                            }

                        }
                        if (flag) {
                            stringBuilder.append(queryCass).append(queryData).append("}");
                        }
                        //dod(document, queryCass_s, queryData, stringBuilder);
                    }
                } else {
                    //分别判断
                    dod(document, queryCass_s, queryData, stringBuilder);
                }
            }
            System.out.println(stringBuilder);
        });
        //System.out.println(document);
        FileUtil.writeFile("/weixin/index.html", document.toString());
    }

    static boolean dod(Document document, String[] queryName, String queryData, StringBuilder stringBuilder) {
        if (queryName == null || queryName.length <= 0)
            return false;
        StringBuilder newQuery = new StringBuilder();
        for (String item : queryName) {
            boolean flag = dod(document, item, null, null);
            if (flag && stringBuilder != null) {
                if (newQuery.length() > 0)
                    newQuery.append(",");
                newQuery.append(item);
            }
        }
        if (stringBuilder != null && newQuery.length() > 0) {
            stringBuilder.append(newQuery).append(queryData).append("}");
            return true;
        }
        return false;
    }

    static boolean dod(Document document, String queryName, String queryData, StringBuilder stringBuilder) {
        if (StringUtil.IsEmpty(queryName))
            return false;
        //
        if (stringBuilder != null && StringUtil.IsEmpty(queryData))
            return false;
        // 判断class 选择器
        if (queryName.startsWith(".")) {
            String name = getHoverName(queryName.substring(1));
            if (name == null)
                return false;
            Elements elements = document.getElementsByClass(name);
            if (elements == null || elements.isEmpty())
                return false;
            if (stringBuilder != null) {
                stringBuilder.append(queryName).append(queryData).append("}");
            }
            return true;
        } else if (queryName.startsWith("#")) {
            String name = getHoverName(queryName.substring(1));
            if (name == null)
                return false;
            Element element = document.getElementById(name);
            if (element == null)
                return false;
        } else {
            // 判断是否存在 body.test 类样式
            String[] temps = StringUtil.StringToArray(queryName, ".");
            if (temps == null || temps.length <= 0)
                return false;
            if (temps.length == 1) { // 标签样式
                String name = getHoverName(temps[0]);
                if (name == null)
                    return false;
                Elements elements = document.getElementsByTag(name);
                if (elements.isEmpty())
                    return false;
                if (stringBuilder != null) {
                    stringBuilder.append(temps[0]).append(queryData).append("}");
                }
                return true;
            } else { // 标签绑定样式
                Elements elements = document.getElementsByTag(temps[0]);
                if (elements.isEmpty())
                    return false;
                for (int i = 1; i < temps.length; i++) {
                    if (!elements.hasClass(temps[i]))
                        return false;
                }
                if (stringBuilder != null) {
                    stringBuilder.append(temps[0]).append(queryData).append("}");
                }
                System.out.println("sssss:" + temps[0]);
                return true;
            }
        }
        return false;
    }

    /**
     * 去除hover 样式
     *
     * @param queryName
     * @return
     */
    private static String getHoverName(String queryName) {
        String[] lastQueryNames = StringUtil.StringToArray(queryName, ":");
        if (lastQueryNames == null || lastQueryNames.length <= 0)
            return null;
        return lastQueryNames[0];
    }
}
